﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace Fighter.Fights.Libraries
{
    public static class TreeViewSerializer
    {
        private static XmlDocument xmlDocument;

        public static void TreeViewToXml(TreeView treeView, String path)
        {
            xmlDocument = new XmlDocument();
            xmlDocument.AppendChild(xmlDocument.CreateElement("root"));
            XmlRecursiveExport(xmlDocument.DocumentElement, treeView.Nodes);
            xmlDocument.Save(path);
        }

        public static void XmlToTreeView(String path, TreeView treeView)
        {
            xmlDocument = new XmlDocument();

            xmlDocument.Load(path);
            treeView.Nodes.Clear();
            XmlRecursiveImport(treeView.Nodes, xmlDocument.DocumentElement.ChildNodes);
        }

        private static XmlNode XmlRecursiveExport(XmlNode nodeElement, TreeNodeCollection treeNodeCollection)
        {
            XmlElement xmlNode = null;
            foreach (TreeNode treeNode in treeNodeCollection)
            {
                if (treeNode is ExtendedTreeNode)
                {
                    var extNode = treeNode as ExtendedTreeNode;

                    xmlNode = xmlDocument.CreateElement("extendedtreenode");
                    xmlNode.SetAttribute("key", extNode.Key); ;
                    xmlNode.SetAttribute("text", extNode.Text); ;
                    ExportExtendedTreeNode(xmlNode, treeNode as ExtendedTreeNode);
                }
                else
                {
                    xmlNode = xmlDocument.CreateElement("treeviewnode");
                    xmlNode.SetAttribute("text",treeNode.Text );
                    xmlNode.SetAttribute("name",treeNode.Name );
                }

                if (nodeElement != null)
                    nodeElement.AppendChild(xmlNode);

                if (treeNode.Nodes.Count > 0)
                {
                    XmlRecursiveExport(xmlNode, treeNode.Nodes);
                }
            }
            return xmlNode;
        }

        private static void ExportExtendedTreeNode(XmlElement xmlNode, ExtendedTreeNode extendedTreeNode)
        {
            if (extendedTreeNode.Fighter1 != null)
            {
                var f1 = xmlDocument.CreateElement("fighter1");
                ExportFigher(extendedTreeNode.Fighter1, f1);
                xmlNode.AppendChild(f1);
            }
            if (extendedTreeNode.Fighter2 != null)
            {
                var f2 = xmlDocument.CreateElement("fighter2");
                ExportFigher(extendedTreeNode.Fighter2, f2);
                xmlNode.AppendChild(f2);
            }
        }

        private static void ExportFigher(Fight fight, XmlElement xmlNode)
        {
            xmlNode.SetAttribute("clubmail", fight.ClubMail);
            xmlNode.SetAttribute("idathlete", fight.IDAthlete.ToString());
            xmlNode.SetAttribute("idfight", fight.IDfight.ToString());
            xmlNode.SetAttribute("age", fight.Age.ToString());
            xmlNode.SetAttribute("gender", fight.Gender.ToString());
            xmlNode.SetAttribute("weight", fight.Weight.ToString());
            xmlNode.SetAttribute("belt", fight.Belt.ToString());
            xmlNode.SetAttribute("nfight", fight.NFight.ToString());
            xmlNode.SetAttribute("classes", fight.Classes);
            xmlNode.SetAttribute("agedesc", fight.AgeDesc);
            xmlNode.SetAttribute("weightdesc", fight.WeightDesc);
        }



        private static void XmlRecursiveImport(TreeNodeCollection elem, XmlNodeList xmlNodeList)
        {
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                switch(xmlNode.Name)
                {
                    case "treeviewnode":
                        var treeNode = new TreeNode(xmlNode.Attributes["text"].Value);
                        treeNode.Name = xmlNode.Attributes["name"].Value;
                        if (xmlNode.ChildNodes.Count > 0)
                            XmlRecursiveImport(treeNode.Nodes, xmlNode.ChildNodes);
                        if (elem != null)
                            elem.Add(treeNode);
                        break;
                    case "extendedtreenode":
                        Fight f1 = null, f2 = null; 
                        string key = xmlNode.Attributes["key"].Value;
                        if (xmlNode.ChildNodes.Count > 0)
                        {
                            ReadFight(xmlNode, ref f1, ref f2);
                            var extreeNode = new ExtendedTreeNode(key,f1,f2);
                            if (elem != null)
                                elem.Add(extreeNode);
                        }
                        break;
                }
            }
        }

        private static void ReadFight(XmlNode myXmlNode, ref Fight f1, ref Fight f2)
        {
            foreach (XmlNode xmlNode in myXmlNode.ChildNodes)
            {
                switch (xmlNode.Name)
                {
                    case "fighter1":
                        f1 = new Fight();
                        ImportFigher(xmlNode, f1);
                        break;
                    case "fighter2":
                        f2 = new Fight();
                        ImportFigher(xmlNode, f2);
                        break;
                }
            }
        }

        private static void ImportFigher(XmlNode xmlNode, Fight fight)
        {
            fight.ClubMail = xmlNode.Attributes["clubmail"].Value;
            fight.IDAthlete = int.Parse(xmlNode.Attributes["idathlete"].Value);
            fight.IDfight = int.Parse(xmlNode.Attributes["idfight"].Value);
            fight.Age = int.Parse(xmlNode.Attributes["age"].Value);
            fight.Gender = (Gender) Enum.Parse(typeof(Gender), xmlNode.Attributes["gender"].Value);
            fight.Weight = double.Parse(xmlNode.Attributes["weight"].Value);
            fight.Belt = (Belts) Enum.Parse(typeof(Belts), xmlNode.Attributes["belt"].Value);
            fight.NFight = int.Parse(xmlNode.Attributes["nfight"].Value);
            fight.Classes = xmlNode.Attributes["classes"].Value;
            fight.AgeDesc = xmlNode.Attributes["agedesc"].Value;
            fight.WeightDesc = xmlNode.Attributes["weightdesc"].Value;
        }
    }
}
